package com.xinke.module.system.service.mail;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.xinke.module.system.model.vo.mail.*;
import com.xinke.module.system.model.entity.mail.MailLogDO;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.util.object.BeanUtils;

import com.xinke.module.system.mapper.mail.MailLogMapper;

import static com.xinke.component.common.exception.util.ServiceExceptionUtil.exception;
import static com.xinke.module.system.enums.ErrorCodeConstants.*;

/**
 * 邮件日志 Service 实现类
 *
 * @author yzx
 */
@Service
@Validated
public class MailLogServiceImpl implements MailLogService {

    @Resource
    private MailLogMapper mailLogMapper;

    @Override
    public Long createMailLog(MailLogSaveReqVO createReqVO) {
        // 插入
        MailLogDO mailLog = BeanUtils.toBean(createReqVO, MailLogDO.class);
        mailLogMapper.insert(mailLog);
        // 返回
        return mailLog.getLogId();
    }

    @Override
    public void updateMailLog(MailLogSaveReqVO updateReqVO) {
        // 校验存在
        validateMailLogExists(updateReqVO.getLogId());
        // 更新
        MailLogDO updateObj = BeanUtils.toBean(updateReqVO, MailLogDO.class);
        mailLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteMailLog(Long logId) {
        // 校验存在
        validateMailLogExists(logId);
        // 删除
        mailLogMapper.deleteById(logId);
    }

    private void validateMailLogExists(Long logId) {
        if (mailLogMapper.selectById(logId) == null) {
            throw exception(null);
        }
    }

    @Override
    public MailLogDO getMailLog(Long logId) {
        return mailLogMapper.selectById(logId);
    }

    @Override
    public PageResult<MailLogDO> getMailLogPage(MailLogPageReqVO pageReqVO) {
        return mailLogMapper.selectPage(pageReqVO);
    }

}